﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using Top.Web;
using Top.Web.Api;
using Top.PointStore.Business;
using Top.PointStore.Business.Models;
using Top.PointStore.Web.Filters;
using Top.Web.Api.Filters;
using Top.Core;

namespace Top.PointStore.Web.Controllers
{

    public class InvoiceController : ApiBase
    {

        /// <summary>
        /// 获取供货清单汇总数据
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetList(int cityKid = -1, int orderState = -1, string logisticsNum = "", int pageIndex = 1, int pageSize = 10)
        {
            string startTime = Get("startTime", "");
            string endTime = Get("endTime", "");
            double lgnum = 0;
            if (!string.IsNullOrWhiteSpace(logisticsNum))
            {
                bool bl = Convert3.TryTo<double>(logisticsNum, out lgnum);
                if (!bl)
                {
                    throw new WebApiException("请输入正确的物流单号");
                }
                if (logisticsNum.Length > 16)
                {
                    throw new WebApiException("物流单号长度过长");
                }
            }
            Pager pager = Pager.Create(pageIndex, pageSize);
            return await PointStoreInvoiceManager.FromDBSelectSummary(pager,pUser.PointStoreAuth.F_CityKid, cityKid, orderState, logisticsNum,startTime,endTime);
        }


        public async Task<object> GetDetail(int cityKid = -1, int orderState = -1, int productKid = 0, int pageIndex = 1, int pageSize = 10)
        {
            Pager pager = Pager.Create(pageIndex, pageSize);
            return await PointStoreInvoiceManager.FromDBSelectDetail(pager, cityKid, orderState, productKid);
        }

        public object GetCity()
        {
            return PointStoreInvoiceManager.FromDBGetDespatchCity(pUser.PointStoreAuth);
        }


        public object GetLogisticsNum()
        {
            return PointStoreInvoiceManager.FromDBGetDespatchLogisticsNum(pUser.PointStoreAuth);
        }

        public async Task<object> Edits(string productKids, string createTimes, int orderState, int cityKid, string logisticsNum)
        {
            double lgnum = 0;
            if (!string.IsNullOrWhiteSpace(logisticsNum))
            {
                bool bl = Convert3.TryTo<double>(logisticsNum, out lgnum);
                if (!bl)
                {
                    throw new WebApiException("请输入正确的物流单号");
                }
                if (logisticsNum.Length > 16)
                {
                    throw new WebApiException("物流单号长度过长");
                }
            }
            string[] productKid = productKids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] createTime = createTimes.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                for (int i = 0; i < productKid.Length; i++)
                {
                    await PointStoreInvoiceManager.UpdateToDB(productKid[i], orderState, cityKid, createTime[i], logisticsNum);
                }
                return 1;
            }
            catch
            {
                throw new WebApiException("服务器异常，请联系卡考");
            }            
        }


        public async Task<object> Edit(int productKid, int cityKid, string createTime, string logisticsNum, int top = 1)
        {
            double lgnum = 0;
            if (!string.IsNullOrWhiteSpace(logisticsNum))
            {
                bool bl = Convert3.TryTo<double>(logisticsNum, out lgnum);
                if (!bl)
                {
                    throw new WebApiException("请输入正确的物流单号");
                }
                if (logisticsNum.Length > 16)
                {
                    throw new WebApiException("物流单号长度过长");
                }
            }
            T_PointStoreInvoice model = new T_PointStoreInvoice();
            model.F_CityKid = cityKid;
            model.F_CreateTime = createTime;
            model.F_ProductKid = productKid;
            model.F_OrderState = 0;
            model.F_LogisticsName = logisticsNum;
            return await PointStoreInvoiceManager.Shipments(model, top);
        }

        [Provider]
        public async Task<object> Confirms(string logisticsNum, int cityKid, int productKid = 0)
        {
            return await PointStoreInvoiceManager.UpdateToDB_Confirm(logisticsNum, cityKid, productKid);
        }

        public async Task<object> Backout(int productKid,int cityKid,string createTime,int num=0)
        {
            return await PointStoreInvoiceManager.UpdateToDB_Backout(productKid, cityKid, createTime, num);
        }

        public async Task<string> Export(int orderState, int cityKid)
        {
            string startTime = Get("startTime", "");
            string endTime = Get("endTime", "");
            string logisticsNum = Get("logisticsNum", "");
            return await PointStoreInvoiceManager.FromDBExportDetail(cityKid, orderState,logisticsNum,startTime,endTime);
        }

        public object EditLogisticsNum(string oldnum,string newnum)
        {
            return PointStoreInvoiceManager.UpdateToDB_LogisticsNum(oldnum,newnum,pUser.PointStoreAuth.F_CityKid);
        }
    }
    public class ProductCreateTime
    {
        public int ProductKid { get; set; }

        public string CreateTime { get; set; }
    }
}